<template>
  <x-dialog
    :visible="visible"
    :title="visibleType=='edit'?'编辑':'新增'"
    @close="handleClose(false)"
  >
    <el-form ref="form" inline :model="form" :rules="rules" label-width="80px">
      <el-form-item label="上传歌曲" prop="songUrl" style="display:block">
        <x-upload v-model="form.songUrl" />
      </el-form-item>
      <el-form-item label="歌单" prop="songlistNo">
        <x-select v-model="form.songlistNo" :options="songlistOptions" />
      </el-form-item>
      <el-form-item label="歌手" prop="songerNo">
        <x-select v-model="form.songerNo" :options="songerOptions" />
      </el-form-item>
      <el-form-item label="歌名" prop="songName">
        <el-input v-model="form.songName" clearable />
      </el-form-item>
      <el-form-item label="简介" prop="description" style="display:block">
        <el-input v-model="form.description" cols="70" rows="5" type="textarea" placeholder="请输入内容" clearable />
      </el-form-item>
      <el-form-item label=" ">
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </x-dialog>
</template>

<script>
import * as axiosApi from '../api'
import { getSongerList } from '../../songer/api'
import { getSongList } from '../../songlist/api'
export default {
  props: {
    visible: Boolean,
    visibleType: {
      type: String,
      default: ''
    },
    model: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      form: {
        songName: '',
        description: '',
        songUrl: [],
        songlistNo: '',
        songerNo: ''
      },
      songlistOptions: [],
      songerOptions: [],
      rules: {
        songName: [
          { required: true, message: '请输入歌名' }
        ],
        songUrl: [
          { required: true, message: '请上传歌曲' }
        ],
        songlistNo: [
          { required: true, message: '请选择歌单' }
        ],
        songerNo: [
          { required: true, message: '请选择歌手' }
        ]
      }
    }
  },
  watch: {
    visible(newValue, oldValue) {
      if (newValue) {
        Object.keys(this.form).forEach(key => {
          if (key === 'songUrl' && this.model[key]) {
            this.form[key] = [this.model[key]]
          } else if (this.model[key]) this.form[key] = this.model[key]
        })
      } else {
        this.initForm()
      }
    }
  },
  mounted() {
    this.getSongerOptions()
    this.getSonglistOptions()
  },
  methods: {
    handleClose(update) {
      this.$emit('close', update)
    },
    // 获取歌手列表
    getSongerOptions() {
      getSongerList().then(res => {
        this.songerOptions = res.data.content.map(item => {
          return {
            label: item.name,
            value: item.songerNo
          }
        })
      })
    },
    // 获取歌单列表
    getSonglistOptions() {
      getSongList().then(res => {
        this.songlistOptions = res.data.content.map(item => {
          return {
            label: item.title,
            value: item.songlistNo
          }
        })
      })
    },
    initForm() {
      Object.keys(this.form).forEach(item => {
        this.form[item] = ''
        if (item === 'songUrl') this.form[item] = []
      })
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const fun = this.visibleType === 'edit' ? axiosApi.editSong : axiosApi.addSong
          const params = {
            ...this.form,
            songUrl: this.form.songUrl[0]
          }
          if (this.visibleType === 'edit')params._id = this.model._id
          fun(params).then(() => {
            this.$message({
              message: this.visibleType === 'edit' ? '更新成功' : '新增成功'
            })
            this.handleClose(true)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray:#889aa4;
.show-pwd {
    position: absolute;
    right: 10px;
    top: 2px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
</style>
